package com.example.task1.ui.AllService.PublicBenefit;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.PublicBannerBean;
import com.example.task1.bean.PublicRecommendBean;
import com.example.task1.bean.PublicSortBean;
import com.example.task1.http.Api;
import com.example.task1.ui.service.adapter.RecommendAllServiceAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class PublicBenefitActivity extends AppCompatActivity {

    private Banner banner;
    private RecyclerView rvList;
    private RecyclerView rvRecommend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_benefit);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("爱心公益");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        business();
    }

    private void business() {

    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN,"/prod-api/api/public-welfare/ad-banner/list");
        Api.send(apiRespCall, PublicBannerBean.class, new CallBack<PublicBannerBean>() {
            @Override
            public void success(PublicBannerBean data) {
                List<PublicBannerBean.DataDTO> rows = data.getData();
                List<String> collect = rows.stream().map(PublicBannerBean.DataDTO::getImgUrl).collect(Collectors.toList());
                MyApplication.banner(banner, collect);
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN,"/prod-api/api/public-welfare/public-welfare-activity/recommend-list");
        Api.send(call, PublicRecommendBean.class, new CallBack<PublicRecommendBean>() {
            @Override
            public void success(PublicRecommendBean data) {
                RecommendListAdapter recommendListAdapter = new RecommendListAdapter(PublicBenefitActivity.this);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PublicBenefitActivity.this);
                rvRecommend.setLayoutManager(linearLayoutManager);
                rvRecommend.setAdapter(recommendListAdapter);
                recommendListAdapter.setData(data.getRows());
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> sort = Api.api().get(MyApplication.TOKEN,"/prod-api/api/public-welfare/public-welfare-type/list");
        Api.send(sort, PublicSortBean.class, new CallBack<PublicSortBean>() {
            @Override
            public void success(PublicSortBean data) {
                PublicSortAdapter recommendAllServiceAdapter = new PublicSortAdapter(PublicBenefitActivity.this);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(PublicBenefitActivity.this,4);
                recommendAllServiceAdapter.setData(data.getData());
                rvList.setLayoutManager(gridLayoutManager);
                rvList.setAdapter(recommendAllServiceAdapter);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        rvList = (RecyclerView) findViewById(R.id.rv_list);
        rvRecommend = (RecyclerView) findViewById(R.id.rv_recommend);
    }
}